﻿package  com.devilcat.ws{
	import com.devilcat.ws.Basicws;
	import com.devilcat.ws.UserPokemonws;
	import com.worlize.websocket.*;
	import com.devilcat.dt.Gamedt;
	
	public class Loginws extends Basicws{
		var mUsername:String;
		var mPassword:String;
		public function Loginws(_username:String,_password:String) {			
			super("iw-protocol");
			mUsername=_username;
			mPassword=_password;
			// constructor code
		}
		override function handleWebSocketOpen(event:WebSocketEvent):void
		{
			var pack:Object = new Object();
			pack.username=pureString(mUsername);
			pack.password=pureString(mPassword);
			pack.token = Math.round(Math.random()*10000)
			Gamedt.Player.token=pack.token;
			pack.action="login";
			var myJson:String = JSONString(pack);
			websocket.sendUTF(myJson);
		}
		override function handleWebSocketMessage(event:WebSocketEvent):void
		{
			if (event.message.type === WebSocketMessage.TYPE_UTF8)
			{
				trace("Login: " + event.message.utf8Data);
				var longinData:Object = JSONObject(event.message.utf8Data)
				if(longinData.error=="login"){
					Gamedt.MainUI.preLoadtxt.text="Load Pokemon";
					Gamedt.Player.setData(longinData);
					loadUserPokemonList();
				}else{
					Gamedt.MainUI.preLoadtxt.text="Login Error";
					//Thong bao dang nhap sai;
				}
				debug(longinData.flag);
				//Gamedt.loging=false;
			}
			closeConnect();
		}
		function loadUserPokemonList(){
			var lupl:UserPokemonws = new UserPokemonws(Gamedt.Player.uid);
			
		}

	}
	
}
